# pagination.py 分页器
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


class StandardPagination(PageNumberPagination):
    page_size_query_param = 'page_size'
    max_page_size = 100

    def get_page_number(self, request, paginator):
        # 优先使用请求参数中的page值
        return request.query_params.get(self.page_query_param, 1)

    def get_paginated_response(self, data):
        return Response({     # 使用 DRF的 Response对象包装返回值
            'count': self.page.paginator.count,
            'results': data,
            'next': self.get_next_link(),
            'previous': self.get_previous_link()
        })
